#!/usr/bin/python2.7.15
# -*- coding: utf-8 -*-  
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import sys
import csv

url=sys.argv[1]
myList=[]

# driver = webdriver.Chrome()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
# driver = webdriver.Firefox()
print("================Before request================")
print("")
# driver.get("https://blog.csdn.net/qq_28618765/article/details/78085457")
# driver.get("https://www.hotdeals.com/")
driver.get(url)
print(driver.current_url)
print("")
print("================End request================")
print("")
print("")
		
print("================Check rel='canonical' ================")
print("")
try:
	canonical_ele=driver.find_element_by_css_selector('link[rel="canonical"]');
except :
    print 'No canonical Element'
    myList.append(['canonical',"NO"])
else:
	print('rel="canonical" element is:');
	myList.append(['rel="canonical" element is:'])
	canonical_ele_outerHTML=canonical_ele.get_attribute('outerHTML')
	print("\t"+canonical_ele_outerHTML)
	myList.append(["",canonical_ele_outerHTML])
print("")	
print("================End Check rel='canonical' ================")
print("")
print("")

print("================Check meta description ================")
print("")
try:
	meta_description_ele=driver.find_element_by_css_selector('meta[name="description"]');
except :
    print 'No Meta name="description" Element'
    myList.append(['Meta description Element','NO'])
else:
	print('Meta name="description" element is:');
	myList.append(['Meta name="description" element is:'])
	meta_description_ele_outerHTML=meta_description_ele.get_attribute('outerHTML')
	print("\t"+meta_description_ele_outerHTML)
	myList.append(["",meta_description_ele_outerHTML])
print("")
print("================End Check meta description ================")
print("")
print("")

print("================Check meta keywords ================")
print("")
try:
	meta_keywords_ele=driver.find_element_by_css_selector('meta[name="keywords"]');
except :
    print 'No Meta name="keywords" Element'
    myList.append(['Meta keywords Element','No'])
else:
	print('Meta name="keywords" Element is:');
	myList.append(['Meta name="keywords" Element is:'])
	meta_keywords_ele_outerHTML=meta_keywords_ele.get_attribute('outerHTML')
	print("\t"+meta_keywords_ele_outerHTML)
	myList.append(["",meta_keywords_ele_outerHTML])
print("")
print("================End Check meta keywords ================")
print("")
print("")

print("================Check title ================")
print("")
try:
	title_ele=driver.find_element_by_tag_name('title');
except :
    print 'No title Element'
    myList.append(['title Element','No'])
else:
	print("title element is:");
	myList.append(["title element is:"])
	title_ele_outerHTML=title_ele.get_attribute('outerHTML')
	print("\t"+title_ele_outerHTML)
	myList.append(["",title_ele_outerHTML])
print("")	
print("================End Check title ================")
print("")
print("")


print("================Check H label ================")
print("")
try:
	h1_eles=driver.find_elements_by_tag_name('H1');
except :
    print 'No H1 Element'
    myList.append(['H1 Element','No'])
else:
	if len(h1_eles):
		print 'H1 Elements is:'
		myList.append(['H1 Elements is:'])
		for t in h1_eles:
		    h1_eles_innerHTML=t.get_attribute("innerHTML")
		    print("\t"+h1_eles_innerHTML)
		    myList.append(["",h1_eles_innerHTML])
		    print("")
	else :
		print 'No H1 Element'
		myList.append(['H1 Element','No'])

try:
	h2_eles=driver.find_elements_by_tag_name('H2');
except :
    print 'No H2 Element'
    myList.append(['H2 Element','NO'])
else:
	if len(h2_eles):
		print 'H2 Elements is:'
		myList.append(['H2 Elements is:'])
		for t in h2_eles:
		    h2_eles_innerHTML=t.get_attribute("innerHTML")
		    print("\t"+h2_eles_innerHTML)
		    myList.append(["",h2_eles_innerHTML])
		    print("")
	else :
		print 'No H2 Element'
		myList.append(['H2 Element','NO'])

try:
	h3_eles=driver.find_elements_by_tag_name('H3');
except :
    print 'No H3 Element'
    myList.append(['H3 Element','No'])
else:
	if len(h3_eles):
		print 'H3 Elements is:'
		myList.append(['H3 Elements is:'])
		for t in h3_eles:
		    h3_eles_innerHTML=t.get_attribute("innerHTML")
		    print("\t"+h3_eles_innerHTML)
		    myList.append(["",h3_eles_innerHTML])
		    print("")
	else :
		print 'No H3 Element'
		myList.append(['H3 Element','No'])

try:
	h4_eles=driver.find_elements_by_tag_name('H4');
except :
    print 'No H4 Element'
    myList.append(['H4 Element','No'])
else:
	if len(h4_eles):
		print 'H4 Elements is:'
		myList.append(['H4 Elements is:'])
		for t in h4_eles:
		    h4_eles_innerHTML=t.get_attribute("innerHTML")
		    print("\t"+h4_eles_innerHTML)
		    myList.append(["",h4_eles_innerHTML])
		    print("")
	else :
		print 'No H4 Element'
		myList.append(['H4 Element','No'])
try:
	h5_eles=driver.find_elements_by_tag_name('H5');
except :
    print 'No H5 Element'
    myList.append(['H5 Element','No'])
else:
	if len(h5_eles):
		print 'H5 Elements is:'
		myList.append(['H4 Elements is:'])
		for t in h5_eles:
		    h5_eles_innerHTML=t.get_attribute("innerHTML")
		    print("\t"+h5_eles_innerHTML)
		    myList.append(["",h5_eles_innerHTML])
		    print("")
	else :
		print 'No H5 Element'
		myList.append(['H5 Element','No'])
try:
	h6_eles=driver.find_elements_by_tag_name('H6');
except :
    print 'No H6 Element'
    myList.append(['H6 Element','No'])
else:
	if len(h6_eles):
		print 'H6 Elements is:'
		myList.append(['H6 Elements is:'])
		for t in h6_eles:
		    h6_eles_innerHTML=t.get_attribute("innerHTML")
		    print("\t"+h6_eles_innerHTML)
		    myList.append(["",h6_eles_innerHTML])
		    print("")
	else :
		print 'No H6 Element'
		myList.append(['H6 Element','No'])
print("")
print("================End Check H label ================")
print("")
print("")



print("================Check meta ROBOTS ================")
print("")
try:
	meta_rebots_ele=driver.find_element_by_css_selector('meta[name="ROBOTS"]');
except :
    print 'No Meta name="ROBOTS" Element'
    myList.append(['No Meta name="ROBOTS" Element'])
else:
	print('Meta name="ROBOTS" Element is:');
	myList.append(['Meta name="ROBOTS" Element is:'])
	meta_rebots_ele_outerHTML=meta_rebots_ele.get_attribute('outerHTML')
	print("\t"+meta_rebots_ele_outerHTML)
	myList.append(["",meta_rebots_ele_outerHTML])
print("")
print("================End Check meta ROBOTS ================")
print("")
print("")

# print("================Check a rel ================")
# print("")
# try:
# 	a_eles=driver.find_elements_by_tag_name('a');
# except :
#     print 'No a Elements '
# else:
# 	for t in a_eles:
# 	    print(t.get_attribute("href"))
# 	    print(t.get_attribute("rel"))
# 	    print("")
# print("")	
# print("================End a rel ================")
# print("")
# print("")


print("================Check img alt ================")
print("")
try:
	img_eles=driver.find_elements_by_tag_name('img');
except :
    print 'No img Element'
    myList.append(['img Element','No'])
else:
	print("There are "+str(len(img_eles))+" img Elements!");
	myList.append(["There are "+str(len(img_eles))+" img Elements!"])
	img_eles_emptyalt_list=[];
	img_eles_notemptyalt_list=[];
	for t in img_eles:
		if len(t.get_attribute("alt")):
			img_eles_notemptyalt_list.append(t.get_attribute("outerHTML"))
		    # print(t.get_attribute("outerHTML"))
		    # print("\t"+t.get_attribute("alt"))
		else:
			img_eles_emptyalt_list.append(t.get_attribute("outerHTML"))
	# print(img_eles_notemptyalt_list)
	print("There are "+str(len(img_eles_notemptyalt_list))+" img elements have alt!");
	myList.append(["There are "+str(len(img_eles_notemptyalt_list))+" img elements have alt!"])
	if len(img_eles_emptyalt_list):
		print("There are "+str(len(img_eles_emptyalt_list))+" img elements haven't alt!")
		myList.append(["There are "+str(len(img_eles_emptyalt_list))+" img elements haven't alt!"])
		for t in img_eles_emptyalt_list:
			print("\t"+t);
			myList.append(["",t])
	# print(img_eles_emptyalt_list)
	print("")
print("")	
print("================End img alt ================")
print("")
print("")


with open('./t.csv','wb') as myFile:
    myWriter=csv.writer(myFile)    
    myWriter.writerows(myList) 
# # try:
#     # element = WebDriverWait(driver, 10).until(
#     #     EC.presence_of_element_located((By.CSS_SELECTOR, 'link[rel="canonical"]'))
#     # )
#     # print(element.get_attribute('outerHTML'))
# 	# 定位一组元素
# 	# texts = driver.find_elements_by_css_selector();
# 	# 循环遍历出每一条搜索结果的标题
# 	# for t in texts:
# 	#     print(t.get_attribute("href"))
# 	#     print(t);
# # finally:
driver.quit()